﻿<%@ Page Language="VB" MasterPageFile="~/Default.master" Strict="false" ValidateRequest="false" Title="Modify Membership Details" %>

<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    Namespace="System.Web.UI" TagPrefix="asp" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<%@ Register Src="~/UserControls/Photos_formpicker.ascx" TagName="Photos_formpicker" TagPrefix="Club" %>

<%@ Import Namespace="System" %>
<%@ Import Namespace="SubSonic" %>
<%@ Import Namespace="ClubStarterKit.Data" %>
<%@ Import Namespace="System.Web.UI.HtmlControls" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<script runat="server">
  
    Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        ClubStarterKit.Web.Members.DeleteUser()
        FormsAuthentication.SignOut()
        Response.Redirect("~/Default.aspx")
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        If Not Page.User.Identity.IsAuthenticated Then
            Response.Write("To use the member details functionality, you need to be authenticated. Please use the <a href='login.aspx'>Login</a> page to authenticate yourself.")
            Response.End()
        End If
        If Not IsPostBack Then
            InitPageData()
        End If
    End Sub
  
    Protected Sub update_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim user As MembershipUser = Membership.GetUser()
           
        If Email.Text <> user.Email Then
            user.Email = Email.Text
            Membership.UpdateUser(user)
        End If
        Try
            Dim meminf As New MemberInfo(MemberInfo.Columns.Memberid, user.ProviderUserKey())
            If meminf.IsLoaded = True Then
                Dim qry As New Query(Tables.MemberInfo)
                qry.QueryType = QueryType.Update
                qry.AddWhere(MemberInfo.Columns.Memberid, user.ProviderUserKey)
                qry.AddUpdateSetting(MemberInfo.Columns.Address, Addr.Text)
                qry.AddUpdateSetting(MemberInfo.Columns.Firstname, fname.Text)
                qry.AddUpdateSetting(MemberInfo.Columns.Lastname, lname.Text)
                qry.AddUpdateSetting(MemberInfo.Columns.Phone, Phone.Text)
                qry.AddUpdateSetting(MemberInfo.Columns.Newsletter, NewsletterCheck.Checked)
                qry.Execute()
                ContactStatus.Text = "Details have been updated sucessfully."
                ContactStatus.ControlStyle.ForeColor = Drawing.Color.Black
            Else
                MemberInfo.Insert(Addr.Text, Phone.Text, fname.Text, lname.Text, Nothing, user.ProviderUserKey(), NewsletterCheck.Checked)
                ContactStatus.Text = "Details have been updated sucessfully."
                ContactStatus.ControlStyle.ForeColor = Drawing.Color.Black
            End If
        Catch ex As Exception
            ContactStatus.Text = "Error updating contact details: " & ex.Message
            ContactStatus.ControlStyle.ForeColor = Drawing.Color.Red
        End Try
    End Sub
    
    Sub InitPageData()
        Dim user As MembershipUser = Membership.GetUser()
        
        Dim MemInfo As New MemberInfo(MemberInfo.Columns.Memberid, user.ProviderUserKey)
        Dim MemProf As New MemberProfile(MemberProfile.Columns.Memberid, user.ProviderUserKey())
        
        If MemInfo.IsLoaded = True Then
            UserName.Text = user.UserName
            fname.Text = MemInfo.Firstname
            lname.Text = MemInfo.Lastname
            Email.Text = user.Email
            Addr.Text = MemInfo.Address
            Phone.Text = MemInfo.Phone
            Dim newsletter As Boolean = CBool(MemInfo.Newsletter)
            If newsletter = True Then
                NewsletterCheck.Checked = True
            Else
                NewsletterCheck.Checked = False
            End If
            If Not IsDBNull(MemInfo.Avatar) Then
                avatarimage.ImageUrl = "~\UserControls\AvatarImagefetch.ashx?Memberid=" & user.ProviderUserKey.ToString
            End If
        End If
        
        If MemProf.IsLoaded = True Then
            SignatureTextBox.Value = MemProf.Signature
            BioTextBox.Value = MemProf.Bio
            ShowEmailCheckBox.Checked = MemProf.Showemail
            MsgsPerPageTextBox.Text = MemProf.Msgsperpage.ToString()
            SortDescendingCheckBox.Checked = MemProf.Sortdesc
            ShowSignaturesCheckBox.Checked = MemProf.Showsignatures
            ShowAvatarsCheckBox.Checked = MemProf.Showavatars
            SendWatchEmailsCheckBox.Checked = MemProf.Sendwatchemails
        End If
        
    End Sub

    Protected Sub uploadimage_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        If String.IsNullOrEmpty(newavatar.FileName) OrElse newavatar.PostedFile Is Nothing Then Return
        Dim thumbimage As Byte() = ImageUtils.MakeThumb(newavatar.FileBytes, 69, 69)
        UpdateAvatar(thumbimage)
        avatarimage.ImageUrl = "~\UserControls\AvatarImagefetch.ashx?Memberid=" & Membership.GetUser().ProviderUserKey.ToString()
    End Sub
    
    Protected Sub clearimage_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        UpdateAvatar(Nothing)
        avatarimage.ImageUrl = "~/images/nophoto.gif"
    End Sub
    
    Public Sub UpdateAvatar(ByVal Avatar As Byte())
        Dim memberid As Guid = CType(Membership.GetUser().ProviderUserKey, Guid)
        Dim qry As New Query(Tables.MemberInfo)
        qry.QueryType = QueryType.Update
        qry.AddWhere(MemberInfo.Columns.Memberid, memberid)
        If Avatar Is Nothing Then
            
        Else
            qry.AddUpdateSetting(MemberInfo.Columns.Avatar, Avatar)
        End If
        qry.Execute()
    End Sub

    Protected Sub UpdateProfileButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim user As MembershipUser = Membership.GetUser()
        
        Dim MemProf As New MemberProfile(MemberProfile.Columns.Memberid, user.ProviderUserKey())
        Try
            If MemProf.IsLoaded = True Then
                Dim Mem As New Member()
                Mem.Signature = SignatureTextBox.Value
                Mem.Bio = BioTextBox.Value
                Mem.ShowEmail = ShowEmailCheckBox.Checked
                Mem.MsgsPerPage = MsgsPerPageTextBox.Text
                Mem.SortDescending = SortDescendingCheckBox.Checked
                Mem.ShowSignatures = ShowSignaturesCheckBox.Checked
                Mem.ShowAvatars = ShowAvatarsCheckBox.Checked
                Mem.SendWatchEmails = SendWatchEmailsCheckBox.Checked
                Mem.MemberId = user.ProviderUserKey()
                ClubStarterKit.Data.Member.Update(Mem)
                ProfStatus.Text = "Profile has been updated sucessfully."
                ProfStatus.ControlStyle.ForeColor = Drawing.Color.Black
            Else
                MemberProfile.Insert(user.ProviderUserKey, DateTime.Now(), SignatureTextBox.Value, BioTextBox.Value, SortDescendingCheckBox.Checked, MsgsPerPageTextBox.Text, ShowAvatarsCheckBox.Checked, ShowSignaturesCheckBox.Checked, SendWatchEmailsCheckBox.Checked, ShowEmailCheckBox.Checked)
                ProfStatus.Text = "Profile has been updated sucessfully."
                ProfStatus.ControlStyle.ForeColor = Drawing.Color.Black
            End If
        Catch ex As Exception
            ProfStatus.Text = "Error updating profile: " & ex.Message
            ProfStatus.ControlStyle.ForeColor = Drawing.Color.Red
        End Try
        
    End Sub
</script>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <div id="body">
        <div id="columnleft">
            <a name="content_start" id="content_start"></a>
            <div class="leftblock">
                <h2>
                    Membership Details for
                    <asp:Label ID="UserName" runat="server" /></h2>
            </div>
            <div class="leftblock">
            <h2>
            Delete your membership
            </h2>
        <div class="dashedline">
        </div>
        <p>
            You can cancel your membership in this club by clicking on the button below. If
            you cancel your membership, you will be removed from the roster and will no longer
            receive emails from the club leadership.
        </p>
        <Club:RolloverButton runat="server" ID="btnCancel" Text="Remove" OnClick="btnCancel_Click" />
        <cc1:ConfirmButtonExtender ID="ConfirmButtonExtender1" runat="server" TargetControlID="btnCancel" ConfirmText="Are you sure you want to delete your membership?">
        </cc1:ConfirmButtonExtender>
    </div>
        </div>
        <div id="columnright">
            <div class="rightblock">
                <h2>
                    <asp:ScriptManager id="ScriptManager1" runat="server">
                    </asp:ScriptManager>
                    Modify Contact Details
                </h2>
                <div class="dashedline">
                </div>
                <p>
                    Use the form below to change your contact details. These values are used in the
                    members list, and for club leadership to contact you.
                </p><p>
</p>
              <asp:UpdatePanel ID="up1" runat="server"><ContentTemplate>
                    <table border="0">
                        <tbody>
                            <tr>
                                <td style="WIDTH: 138px" class="formlabel">
                                    <label for="Email">
                                        <strong>E-mail:</strong></label></td>
                                <td>
                                    <asp:TextBox ID="Email" runat="server" CssClass="txtfield"></asp:TextBox>
                                    <asp:RequiredFieldValidator ID="EmailRequired" runat="server" ControlToValidate="Email"
                                        ErrorMessage="E-mail is required." ToolTip="E-mail is required." ValidationGroup="CreateUserForm">*</asp:RequiredFieldValidator><strong>
                                        </strong>
                                </td>
                            </tr>
                            <tr style="font-weight: bold">
                                <td style="WIDTH: 138px; HEIGHT: 40px" class="formlabel">
                                    <label for="fname">
                                        First Name:</label>
                                </td>
                                <td style="HEIGHT: 40px">
                                    <asp:TextBox ID="fname" runat="server" CssClass="txtfield"></asp:TextBox>
                                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="fname"
                                        ErrorMessage="First Name is required." ToolTip="First Name is required." ValidationGroup="CreateUserForm">*</asp:RequiredFieldValidator>
                                </td>
                            </tr>
                            <tr>
                                <td style="WIDTH: 138px" class="formlabel">
                                    <strong>
                                    <label for="lname">
                                        Last Name:</label>
                                    </strong>
                                </td>
                                <td>
                                    <asp:TextBox ID="lname" runat="server" CssClass="txtfield"></asp:TextBox><strong> </strong>
                                    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="lname"
                                        ErrorMessage="Last Name is required." ToolTip="Last Name is required." ValidationGroup="CreateUserForm">*</asp:RequiredFieldValidator>
                                </td>
                            </tr>
                            <tr>
                                <td style="WIDTH: 138px" class="formlabel">
                                    <strong>
                                    <label for="Addr">
                                        Address:</label>
                                    </strong>
                                </td>
                                <td>
                                    <asp:TextBox ID="Addr" runat="server" CssClass="txtblock" Rows="3" TextMode="MultiLine"></asp:TextBox>
                                </td>
                            </tr>
                            <tr>
                                <td style="WIDTH: 138px" class="formlabel">
                                    <strong>
                                    <label for="Phone">
                                        Phone:</label>
                                    </strong>
                                </td>
                                <td>
                                    <asp:TextBox ID="Phone" runat="server" CssClass="txtfield"></asp:TextBox>
                                </td>
                            </tr>
                            <tr>
                                <td style="WIDTH: 138px" class="formlabel">
                                    <strong>
                                    Receive Newsletter:</strong></td>
                                <td>
                                    <asp:CheckBox ID="NewsletterCheck" runat="server" /></td>
                            </tr>
                        </tbody>
                    </table>
                <p>
                    <asp:Label ID="ContactStatus" runat="server" EnableViewState="False" Font-Bold="True"
                        Font-Underline="True" ForeColor="#FF0033"></asp:Label>
                    <br />
                    <br />
                    <Club:RolloverButton ID="update" runat="server" OnClick="update_Click" Text="Update Info" />
                </p>
                </ContentTemplate></asp:UpdatePanel>
                <asp:UpdateProgress id="UpdateProgress3" runat="server" AssociatedUpdatePanelID="up1"><progresstemplate>
<asp:Image ID="loading1" runat="server" SkinID="loading" />
</progresstemplate>
                </asp:UpdateProgress>
                </div>
                
                <div class="rightblock">
                <asp:UpdatePanel ID="updateprofile" runat="server"><ContentTemplate>
                <table width="100%">
                    <tr>
                        <td colspan="2">
                            <b></b>
                            <h2>
                                <b>Update Profile</b>
                            </h2>
                        </td>
                    </tr>
                    <tr>
                        <td align="right" valign="top">
                            Signature:
                        </td>
                        <td valign="top">
                            <fckeditorv2:fckeditor id="SignatureTextBox" runat="server" basepath="~/FCKeditor/"
                                skinpath="skins/office2003/" toolbarset="Default" usebroncarriagereturn="true"></fckeditorv2:fckeditor>
                        </td>
                    </tr>
                    <tr>
                        <td align="right" valign="top">
                            Bio:
                        </td>
                        <td valign="top" width="80%">
                            <fckeditorv2:fckeditor id="BioTextBox" runat="server" basepath="~/FCKeditor/" skinpath="skins/office2003/"
                                toolbarset="Default" usebroncarriagereturn="true"></fckeditorv2:fckeditor>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <b>Preferences</b>
                        </td>
                    </tr>
                    <tr>
                        <td align="right" valign="top">
                            Show Email Address:
                        </td>
                        <td valign="top">
                            <asp:CheckBox ID="ShowEmailCheckBox" runat="server" Text="Allow others to see my Email Address" />
                        </td>
                    </tr>
                    <tr>
                        <td align="right" valign="top">
                            Messages Per Page:
                        </td>
                        <td valign="top">
                            <asp:TextBox ID="MsgsPerPageTextBox" runat="server" Columns="3">
                                </asp:TextBox>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="MsgsPerPageTextBox"
                                ErrorMessage="RequiredFieldValidator">*</asp:RequiredFieldValidator>
                            <asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="MsgsPerPageTextBox"
                                ErrorMessage="RangeValidator" MaximumValue="100" MinimumValue="1" Type="Integer">Enter an Integer between 1 and 100!</asp:RangeValidator>
                        </td>
                    </tr>
                    <tr>
                        <td align="right" valign="top">
                            Sort Descending:
                        </td>
                        <td valign="top">
                            <asp:CheckBox ID="SortDescendingCheckBox" runat="server" Text="Display Newest Messages First" />
                        </td>
                    </tr>
                    <tr>
                        <td align="right" valign="top">
                            Show Signatures:
                        </td>
                        <td valign="top">
                            <asp:CheckBox ID="ShowSignaturesCheckBox" runat="server" Text="Display User Signatures in Posts" />
                        </td>
                    </tr>
                    <tr>
                        <td align="right" valign="top">
                            Show Avatars:
                        </td>
                        <td valign="top">
                            <asp:CheckBox ID="ShowAvatarsCheckBox" runat="server" Text="Display User Avatars in Posts" />
                        </td>
                    </tr>
                    <tr>
                        <td align="right" valign="top">
                            Send Watch Emails:
                        </td>
                        <td valign="top">
                            <asp:CheckBox ID="SendWatchEmailsCheckBox" runat="server" Text="Email new Messages in Threads I am Watching (If enabled on this site)" />
                        </td>
                    </tr>
                </table>
                <Club:RolloverButton ID="UpdateProfileButton" runat="server" CausesValidation="True" Text="Update Profile" OnClick="UpdateProfileButton_Click" />
            <asp:Label ID="ProfStatus" runat="server" EnableViewState="False" Font-Bold="True"
                        Font-Underline="True" ForeColor="#FF0033"></asp:Label>
                        
                        </ContentTemplate></asp:UpdatePanel>
                        
                        <asp:UpdateProgress ID="updateprofileloading" runat="server" AssociatedUpdatePanelID="updateprofile"><ProgressTemplate>
                        <asp:Image ID="loadingprof" runat="server" SkinID="loading" />
                        </ProgressTemplate></asp:UpdateProgress>
            </div>
            
            <div class="rightblock">
                <h2>
                    Avatar Image
                </h2>
                <div class="dashedline">
                </div>
                <p>
                    You can upload an avatar image that will be shown in the members list. Your current
                    image is:
                </p>
                <p>
                <asp:Image runat="server" SkinID="preview" ID="avatarimage" CssClass="photo" />
                </p>
                <p>
                    To upload a new image, select a file and click on the upload button.</p>
                <p>
                    Filename: <asp:FileUpload ID="newavatar" runat="server" />
                </p>
                
                <p>
                    <Club:RolloverButton ID="uploadimage" runat="server" Text="Upload Image" OnClick="uploadimage_Click" />
                    </p>
                <p>
                    &nbsp;</p>
            </div>
            <div class="rightblock">
                <h2>
                    Change Password
                </h2>
                <div class="dashedline">
                </div>
                <asp:UpdatePanel id="UpdatePanel2" runat="server" RenderMode="Inline">
                    <contenttemplate>
&nbsp;<asp:ChangePassword id="ChangePassword1" runat="server"><ChangePasswordTemplate>
<table cellPadding="1" border="0"><tr><td><table cellPadding="0" border="0"><tr><td align="right"><asp:Label id="CurrentPasswordLabel" runat="server" AssociatedControlID="CurrentPassword" Font-Bold="True">Password:</asp:Label></td><td><asp:TextBox id="CurrentPassword" runat="server" TextMode="Password"></asp:TextBox> <asp:RequiredFieldValidator id="CurrentPasswordRequired" runat="server" ValidationGroup="ChangePassword1" ToolTip="Password is required." ErrorMessage="Password is required." ControlToValidate="CurrentPassword">*</asp:RequiredFieldValidator> </td></tr><tr><td align="right"><asp:Label id="NewPasswordLabel" runat="server" AssociatedControlID="NewPassword" Font-Bold="True">New Password:</asp:Label></td><td><asp:TextBox id="NewPassword" runat="server" TextMode="Password"></asp:TextBox> <asp:RequiredFieldValidator id="NewPasswordRequired" runat="server" ValidationGroup="ChangePassword1" ToolTip="New Password is required." ErrorMessage="New Password is required." ControlToValidate="NewPassword">*</asp:RequiredFieldValidator> </td></tr><tr><td align=right><asp:Label id="ConfirmNewPasswordLabel" runat="server" AssociatedControlID="ConfirmNewPassword" Font-Bold="True">Confirm New Password:</asp:Label></td><td><asp:TextBox id="ConfirmNewPassword" runat="server" TextMode="Password"></asp:TextBox> <asp:RequiredFieldValidator id="ConfirmNewPasswordRequired" runat="server" ValidationGroup="ChangePassword1" ToolTip="Confirm New Password is required." ErrorMessage="Confirm New Password is required." ControlToValidate="ConfirmNewPassword">*</asp:RequiredFieldValidator> </td></tr><tr><td style="HEIGHT: 16px" align="center" colSpan="2"><asp:CompareValidator id="NewPasswordCompare" runat="server" ValidationGroup="ChangePassword1" ErrorMessage="The Confirm New Password must match the New Password entry." ControlToValidate="ConfirmNewPassword" Display="Dynamic" ControlToCompare="NewPassword" Font-Bold="True" Font-Underline="True"></asp:CompareValidator> </td></tr><tr><td style="COLOR: red; HEIGHT: 13px" align=center colSpan=2><asp:Literal id="FailureText" runat="server" EnableViewState="False"></asp:Literal> </td></tr><tr><td style="TEXT-ALIGN: center" colSpan=2><Club:RolloverButton id="ChangePasswordPushButton" runat="server" Text="Change Password" Style="font-size:12px;" ValidationGroup="ChangePassword1" CommandName="ChangePassword"></Club:RolloverButton> </td></tr></table></td></tr></table>
</ChangePasswordTemplate>
<SuccessTemplate>
<table style="BORDER-COLLAPSE: collapse" cellSpacing="0" cellPadding="1" border="0"><tr><td><table cellPadding="0" border="0"><tr><td align="center" colSpan="2">Change Password Complete</td></tr><tr><td>Your password has been changed!</td></tr><tr><td align=right colSpan=2>&nbsp;</td></tr></table></td></tr></table>
</SuccessTemplate>
</asp:ChangePassword>
</contenttemplate>
                
                </asp:UpdatePanel><br />
                &nbsp;<asp:UpdateProgress id="UpdateProgress2" runat="server" AssociatedUpdatePanelID="UpdatePanel2"><progresstemplate>
<asp:Image ID="loading2" runat="server" SkinID="loading" />
</progresstemplate>
                </asp:UpdateProgress>
            </div>
        </div>
        <div class="clear2column">
        </div>
    </div>
</asp:Content>
